//
//  ChannelVC.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ChannelVC.h"
#import "ZWBChannelCell.h"
#import "ChannelModel.h"
#import "ZWBChannelSecVC.h"
#define CHANNELAPI @"http://api.huapipi.com/channel/v2/show/list?deviceId=1826171&uid=1033275"
@interface ChannelVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *channelTableView;
@property(nonatomic, strong)NSMutableArray *channleArr;
@end

@implementation ChannelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.channleArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createData];
}

- (void)createTableView {
    
    self.channelTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.channelTableView];
    self.channelTableView.delegate = self;;
    self.channelTableView.dataSource = self;
    self.channelTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    [self.channelTableView registerClass:[ZWBChannelCell class] forCellReuseIdentifier:@"channelReuse"];
  
}

- (void)createData {
    [ToolNetWorking ToolNetWorking:CHANNELAPI block:^(id result) {
        
        [self.channleArr addObjectsFromArray:[ChannelModel BaseModel:result[@"rows"]]];
        
            [self.channelTableView reloadData];
        
        
    }];
    
    
}


#pragma mark tableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.channleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return VIEW_HEIGHT / 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZWBChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"channelReuse" forIndexPath:indexPath];
    ChannelModel *model = self.channleArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZWBChannelSecVC *secVC = [[ZWBChannelSecVC alloc] init];
    secVC.str = [self.channleArr[indexPath.row] isId];
    [self.navigationController pushViewController:secVC animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self network];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
